<template>
  <div>
       <!-- 新增修改表单 -->
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="100px" style="width: 90%; margin:10px auto;">
      
        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 因子编号 -->
            <el-form-item label="因子编号：" prop="adj_ft_code">
              <el-input v-model="formSubmitDataCom.adj_ft_code" v-bind:disabled='formSubmitData.id!=""'/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 因子名称 -->
            <el-form-item label="因子名称：" prop="adj_ft_name">
              <el-input v-model="formSubmitDataCom.adj_ft_name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 数据库表名 -->
            <el-form-item label="数据库表名：" prop="db_table">
              <el-input v-model="formSubmitDataCom.db_table" />   
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 显示名称 -->
            <el-form-item label="显示名称：" prop="show_name">
              <el-input v-model="formSubmitDataCom.show_name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 显示字段 -->
            <el-form-item label="显示字段：" prop="show_field">
              <el-input v-model="formSubmitDataCom.show_field" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 关联名称 -->
            <el-form-item label="关联名称：" prop="join_name">
              <el-input v-model="formSubmitDataCom.join_name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 关联字段 -->
            <el-form-item label="关联字段：" prop="join_field">
              <el-input v-model="formSubmitDataCom.join_field" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 账户表对应字段 -->
            <el-form-item label="账户表对应字段：" prop="acct_columns">
              <el-input v-model="formSubmitDataCom.acct_columns" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 筛选条件 -->
            <el-form-item label="筛选条件：" prop="sc_str">
              <el-input v-model="formSubmitDataCom.sc_str" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 所属法人 -->
            <el-form-item label="所属法人" prop="lp_code">
              <el-select v-model="formSubmitDataCom.lp_code" class="filter-item" placeholder="Please select"
                         v-bind:disabled='formSubmitData.id!=""'>
                <el-option v-for="item in lpOptions" :key="item.lp_code" :label="item.lp_name" :value="item.lp_code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 因子类型 -->
            <el-form-item label="因子类型" prop="factor_type">
              <el-select v-model="formSubmitDataCom.factor_type" class="filter-item" placeholder="Please select"
                         v-bind:disabled='formSubmitData.id!=""'>
                <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 备注 -->
            <el-form-item label="备注：" prop="remark">
              <el-input type="textarea" v-model="formSubmitDataCom.remark"  placeholder="Please select"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
       <!-- <el-form-item label="Type" prop="type">
          <el-select v-model="formSubmitDataCom.type" class="filter-item" placeholder="Please select">
            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="Date" prop="timestamp">
          <el-date-picker v-model="formSubmitDataCom.timestamp" type="datetime" placeholder="Please pick a date" />
        </el-form-item>

        <el-form-item label="Title" prop="title">
          <el-input v-model="formSubmitDataCom.title" />
        </el-form-item>

        <el-form-item label="Status">
          <el-select v-model="formSubmitDataCom.status" class="filter-item" placeholder="Please select">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="Imp">
          <el-rate v-model="formSubmitDataCom.importance" :colors="['#99A9BF', '#F7BA2A', '#FF9900']" :max="3" style="margin-top:8px;" />
        </el-form-item>
        <el-form-item label="Remark">
          <el-input v-model="formSubmitDataCom.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
const calendarTypeOptions = [
  { key: '1', display_name: '字典' },
  { key: '2', display_name: '规则' }
]

// // arr to obj, such as { CN : "China", US : "USA" }
// const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
//   acc[cur.key] = cur.display_name
//   return acc
// }, {})



export default {
  name: 'submitForm',
  components: {
    //PageHeaderLayout,
  },
  props:{
    dialogFormVisible:{
        type: Boolean,
        default: false
    },
    dialogStatus:{
        type: String,
    },
    // tableColumn: {
    //     type: Array,
    //     default() {
    //         return []
    //     }
    // },
    formSubmitData:{
      type: Object,
      default: function() {
        return {
          id: undefined,
          adj_ft_code: '', 
          adj_ft_name: '', 
          db_table: '', 
          show_name: '', 
          show_field: '', 
          join_name: '', 
          join_field: '', 
          acct_columns: '', 
          sc_str: '', 
          lp_code: '', 
          remark: '',
        }
      }
    },
    lpOptions: {
      type: Array
    }
  },
  // 定义属性
  data() {
    return {
      textMap: {
          update: '修改',
          create: '新增'
      },
      rules: {
        adj_ft_code: [{ required: true, message: '因子编号必填', trigger: 'change' }],
        adj_ft_name: [{ required: true, message: '因子名称必填', trigger: 'change' }],
        db_table: [{ required: true, message: '数据库表名必填', trigger: 'change' }],
        show_name: [{ required: true, message: '显示名称必填', trigger: 'change' }],
        show_field: [{ required: true, message: '显示字段必填', trigger: 'change' }],
        join_name: [{ required: true, message: '关联名称必填', trigger: 'change' }],
        join_field: [{ required: true, message: '关联字段必填', trigger: 'change' }],
        acct_columns: [{ required: true, message: '账户表对应字段必填', trigger: 'change' }],
        lp_code: [{ required: true, message: '所属法人必填', trigger: 'change' }],
        // type: [{ required: true, message: 'type is required', trigger: 'change' }],
        // timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        // title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      // 表单用到的值
      calendarTypeOptions,
      // statusOptions: ['published', 'draft', 'deleted'],
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom:{
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom:{
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    resetFormData(){
      this.$emit('resetFormDataEmit')
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          // createArticle(this.temp).then(() => {
          //   this.list.unshift(this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Created Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$delete(this.formSubmitData, "_XID");
          const tempData = Object.assign({}, this.formSubmitData)
          this.$emit('handleFromUpdateDataEmit')
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          // updateArticle(tempData).then(() => {
          //   const index = this.list.findIndex(v => v.id === this.temp.id)
          //   this.list.splice(index, 1, this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Update Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  
</style>